package com.digitalchina.lxnsy.zbsy;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.fragment.MainHomePageV3Fragment;
import com.digitalchina.lxnsy.sy.main.Main2Fragment;
import com.digitalchina.lxnsy.sy.main.Main3Fragment;
import com.digitalchina.lxnsy.sy.main.Main4Fragment;
import com.digitalchina.lxnsy.view.NoScrollViewPager;
import com.digitalchina.lxnsy.xml.RongCloudXML;
import com.digitalchina.lxnsy.xml.UserXML;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.lzy.okhttputils.model.HttpParams;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class ZMainActivity extends BaseActivity {
    @Bind(R.id.viewPager)
    NoScrollViewPager viewPager;
    @Bind(R.id.navigation)
    BottomNavigationView navigation;

    private List<Fragment> mFragmentList = new ArrayList<>();
    private int[] navigationId;
    private FragmentAdapter adapter;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            for (int i = 0; i < navigationId.length; i++) {
                if (item.getItemId() == navigationId[i]) {
                    viewPager.setCurrentItem(i);
                    return true;
                }
            }
            return false;
        }
    };


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_main_z);
        hideActionBar();
    }

    @Override
    public void initMember() {


        MainHomePageV3Fragment  f1 = new MainHomePageV3Fragment();
        Main3Fragment f2 = new Main3Fragment();
        Main4Fragment f3 = new Main4Fragment();


        //用户类型 200-农户用户 201-政府监管用户 202-企业用户 203-加工厂用户
        if (UserXML.getLoginStatus(getApplication()).equals(UserXML.USER_IS_LOGIN_VALUE)) {
            if ("null".equals(RongCloudXML.getSubjectenterprisetype(getApplication(), "subjectEnterpriseType"))) {
                navigationId = new int[]{R.id.nav1, R.id.nav2,R.id.nav3};
                navigation.inflateMenu(R.menu.navigation);
                mFragmentList.add(f1);
                mFragmentList.add(f2);

            } else {
                navigationId = new int[]{R.id.nav1, R.id.nav2,R.id.nav3,R.id.nav4};
                navigation.inflateMenu(R.menu.navigation_statistics);
                Main2Fragment f4 = new Main2Fragment();

                mFragmentList.add(f1);
                mFragmentList.add(f4);
                mFragmentList.add(f2);

            }
        } else {

            navigationId = new int[]{R.id.nav1, R.id.nav2,R.id.nav3};
            navigation.inflateMenu(R.menu.navigation);
            mFragmentList.add(f1);
            mFragmentList.add(f2);
        }

        mFragmentList.add(f3);


        adapter = new FragmentAdapter(getSupportFragmentManager(), mFragmentList);
        viewPager.setOffscreenPageLimit(mFragmentList.size());
        viewPager.setAdapter(adapter);


        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }



    @Override
    public void addListener() {

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                navigation.setSelectedItemId(navigationId[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0:
                        callBack(message.obj);
                        break;
                    case MSG_F_0:
                        break;
                    case MSG_S_2:
                        Log.d(TAG, "Set alias in handler.");
                        // 调用 JPush 接口来设置别名。
                        JPushInterface.setAlias(getApplicationContext(),
                                (String) message.obj,
                                mAliasCallback);
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {


    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

//        if (UserXML.getIsLogin(activity)) {
//            String alias = UserXML.getUserAccId(activity).replace("-", "");
//            setAlias(alias);
//        }
    }


    private String TAG = "JPush";
    // 这是来自 JPush Example 的设置别名的 Activity 里的代码。一般 App 的设置的调用入口，在任何方便的地方调用都可以。
    private void setAlias(String alias) {

     //   JPushInterface.setAlias(ZMainActivity.this, 0, alias);
        // 调用 Handler 来异步设置别名
//        mHandler.sendMessage(mHandler.obtainMessage(MSG_S_2, alias));
    }

    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            String logs ;
            switch (code) {
                case 0:
                    logs = "Set tag and alias success";
                    Log.i(TAG, logs);
                    // 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
                    Log.i(TAG, logs);
                    // 延迟 60 秒来调用 Handler 设置别名
                    mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_S_2, alias), 1000 * 60);
                    break;
                default:
                    logs = "Failed with errorCode = " + code;
                    Log.e(TAG, logs);
            }
        }
    };
}
